package connotation.ui

import android.graphics.Color
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import cn.feng.skin.manager.base.SkinBaseActivity
import cn.feng.skin.manager.listener.ILoaderListener
import cn.feng.skin.manager.load.SkinManager
import com.zyf.connotation.R
import com.zyf.connotation.utils.ShardUtil
import connotation.utils.AssetsUtils
import kotlinx.android.synthetic.main.activity_skin.*
import kotlinx.android.synthetic.main.skin_item.view.*

class SkinActivity : SkinBaseActivity() {
    var colorName = listOf("默认","浪漫粉","天空蓝","高贵棕","酷炫黑")
    var colorInt = listOf("#303F9F","#FA7198","#08B5F1","#4e342e","#000000")
    var skinname = listOf("","pink.skin","sky.skin","brown.skin","black.skin")
    var check = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_skin)
        dynamicAddSkinEnableView(toolbar, "background", R.color.colorPrimary)
        initData()
    }
    fun initData(){
        check = ShardUtil.getPreferenceInt(applicationContext,"skincheck")
        setSupportActionBar(toolbar)
        supportActionBar?.setHomeButtonEnabled(true)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        sink_recycle.layoutManager = GridLayoutManager(this@SkinActivity,3)
        sink_recycle.adapter = SkinAdapter()
    }
    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        if(item?.itemId==android.R.id.home){
            finish()
        }
        return super.onOptionsItemSelected(item)
    }
    inner class SkinAdapter : RecyclerView.Adapter<SkinHolder>(){
        override fun getItemCount(): Int {
           return colorName.size
        }

        override fun onBindViewHolder(holder: SkinHolder?, position: Int) {
            holder?.colorView?.setBackgroundColor(Color.parseColor(colorInt[position]))
            if(check == position){
                holder?.check_img?.visibility = View.VISIBLE
            }else{
                holder?.check_img?.visibility = View.GONE
            }
            holder?.colorName?.text = colorName[position]
            holder?.itemView?.setOnClickListener {
                if(position==0){
                    SkinManager.getInstance().restoreDefaultTheme()
                }else{
                    setSkin(skinname[position])
                }
                check = position
                ShardUtil.savePreferenceInt(applicationContext,"skincheck",position)
                notifyDataSetChanged()
            }
        }

        override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): SkinHolder {
           return SkinHolder(View.inflate(this@SkinActivity,R.layout.skin_item,null))
        }
    }
    inner class SkinHolder(itemView: View?):RecyclerView.ViewHolder(itemView){
        var colorView = itemView?.colorView
        var check_img = itemView?.check_img
        var colorName = itemView?.colorName
    }
    fun setSkin(skin:String){
        SkinManager.getInstance().load(AssetsUtils.dir + "/"+skin,object : ILoaderListener {
            override fun onStart() {
            }

            override fun onSuccess() {
                Toast.makeText(this@SkinActivity,"换肤成功",Toast.LENGTH_SHORT).show()
            }

            override fun onFailed() {}
        })
    }

}
